﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Nirvana
{
    public class PlayerAudioSequence : IPlayerAudio
    {
        private AudioItem audioItem;
        private AudioSourcePool audiopool;
        private AudioSource audioSource;
        private AudioSourceCtrl audioSourceCtl;
        private AudioSubItemData[] audioSubItems;
        private bool isStop;
        private int sequenceindex;
        private Transform root;

        public PlayerAudioSequence(AudioItem item,AudioSourcePool pool,AudioSource source, AudioSubItemData[] subs)
        {
            this.audioItem = item;
            this.audiopool = pool;
            this.audioSource = source;
            this.audioSourceCtl = new AudioSourceCtrl(source, 0, 0, 0f);
            this.audioSubItems = subs;
        }

        public override string ToString()
        {
            return this.audioItem.name;
        }

        public bool IsPlaying { get { return !this.isStop || this.audioSourceCtl.IsPlaying(); } }

        public float GetRamingTime()
        {
            if (this.isStop) return 0f;
            return this.audioSourceCtl.GetClipLength() - this.audioSourceCtl.GetPlayingTime();
        }

        public void Play()
        {
            this.isStop = false;
            this.sequenceindex = 0;
            StartPlay();
        }

        public void Remove()
        {
            if (this.audioItem != null) this.audioItem.Remove(this);
            if (this.audiopool != null) this.audiopool.Push(this.audioSource);
        }

        public void Set(Vector3 pos)
        {
            this.audioSource.transform.position = pos;
        }

        public void Set(Transform root)
        {
            this.root = root;
        }

        public void Stop()
        {
            this.isStop = true;
            this.audioSourceCtl.Stop();
        }

        public void Update()
        {
            if (isStop && !this.audioSourceCtl.IsPlaying()) return;

            if (this.root != null)
            {
                this.audioSource.transform.SetPositionAndRotation(this.root.position, this.root.rotation);
                this.audioSource.transform.localScale = this.root.localScale;
            }
            this.audioSourceCtl.UpdateVolume();
            float ramingTime = this.audioSourceCtl.GetClipLength() - this.audioSourceCtl.GetPlayingTime();
            if(ramingTime <= this.audioSourceCtl.FadeOutTime && !this.audioSourceCtl.IsStop())
            {
                this.audioSourceCtl.Stop();
            }
            if(!this.isStop && !this.audioSourceCtl.IsPlaying())
            {
                this.sequenceindex = (this.sequenceindex + 1) % this.audioSubItems.Length;
                StartPlay();
            }


        }

        public IEnumerator WaitFinish()
        {
            return new WaitUntil(() => !this.IsPlaying);
        }

        private void StartPlay()
        {
            var subitem = this.audioSubItems[this.sequenceindex];
            this.audioItem.InitAudioSource(this.audioSource, subitem);
            this.audioSourceCtl.DelayTime = this.audioItem.Delay + subitem.GetDelay();
            this.audioSourceCtl.FadeInTime = subitem.GetFadeInTime();
            this.audioSourceCtl.FadeOutTime = subitem.GetFadeOutTime();
            this.audioSourceCtl.Play();
        }
    }

}

